package com.genesis.web.controller;



import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.genesis.mall.common.Result;
import com.genesis.mall.dto.SmsFlashPromotionSessionDto;
import com.genesis.mall.entity.*;
import com.genesis.mall.service.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 前台首页内容
 */

@RestController
@RequestMapping("/home")
public class HomeController {


    @Autowired
    private ISmsHomeAdvertiseService smsHomeAdvertiseService;

    @Autowired
    private IUmsMemberService umsMemberService;

    @Autowired
    private ISmsHomeBrandService iSmsHomeBrandService;

    @Autowired
    private IPmsBrandService pmsBrandService;


    @Autowired
    private ISmsHomeRecommendProductService recommendProductService;

    @Autowired
    private ISmsHomeNewProductService newProductService;

    @Autowired
    private IPmsProductService productService;
    //秒杀
    @Autowired
    private ISmsFlashPromotionSessionService iSmsFlashPromotionSessionService;

    @Autowired
    private ISmsFlashPromotionProductRelationService iSmsFlashPromotionProductRelationService;

    @Autowired
    private IPmsProductService iPmsProductService;

    @Autowired
    private ICmsSubjectService iCmsSubjectService;

    @Autowired
    private ISmsHomeRecommendSubjectService iSmsHomeRecommendSubjectService;

    /**
     * 首页滚动
     *
     * @param
     * @return
     */

    @GetMapping("/content")
    public Result context() {


        //1. 轮播广告

        ArrayList<SmsHomeAdvertise> advertiseList1 = new ArrayList<>();


        LambdaQueryWrapper<SmsHomeAdvertise> queryWrapper2 = new LambdaQueryWrapper<>();
        queryWrapper2.orderByAsc(SmsHomeAdvertise::getSort);
        queryWrapper2.eq(SmsHomeAdvertise::getStatus, 1);
//        queryWrapper2.eq(SmsHomeAdvertise::getType, 1);
        List<SmsHomeAdvertise> advertiseList = smsHomeAdvertiseService.list(queryWrapper2);

        for (SmsHomeAdvertise smsHomeAdvertise : advertiseList) {
            if (smsHomeAdvertise != null && smsHomeAdvertise.getStatus() == 1) {
                advertiseList1.add(smsHomeAdvertise);
            }

        }


        ArrayList<SmsHomeAdvertise> list5 = new ArrayList<>();

        if (advertiseList1.size() > 5) {
            for (int i = 0; i < 5; i++) {
                list5.add(advertiseList1.get(i));
            }

        } else {
            list5.addAll(advertiseList1);
        }

//整体复制集合


        //2.   推荐品牌

        LambdaQueryWrapper<SmsHomeBrand> queryWrapper7 = new LambdaQueryWrapper<>();
        queryWrapper7.orderByAsc(SmsHomeBrand::getSort);
        queryWrapper7.eq(SmsHomeBrand::getRecommendStatus, 1);
        List<SmsHomeBrand> brandLists = iSmsHomeBrandService.list(queryWrapper7);


        ArrayList<PmsBrand> brandList = new ArrayList<>();

        for (SmsHomeBrand smsHomeBrand : brandLists) {
            Long brandId = smsHomeBrand.getBrandId();
            LambdaQueryWrapper<PmsBrand> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(StringUtils.isNotEmpty(brandId.toString()), PmsBrand::getId, brandId);
            queryWrapper.orderByAsc(PmsBrand::getSort);
            queryWrapper.eq(PmsBrand::getFactoryStatus, 1);//改动
            queryWrapper.eq(PmsBrand::getShowStatus, 1);
            PmsBrand brand = pmsBrandService.getOne(queryWrapper);//改动
            brandList.add(brand);
        }

        ArrayList<PmsBrand> list4 = new ArrayList<>();
        if (brandList.size() > 6) {

            for (int i = 0; i < 6; i++) {
                list4.add(brandList.get(i));
            }
        } else {
            list4.addAll(brandList);
        }


        //2.1 秒杀


        SmsFlashPromotionSessionDto smsFlashPromotionSessionDto = new SmsFlashPromotionSessionDto();


        List<SmsFlashPromotionSession> list1 = iSmsFlashPromotionSessionService.list();
        for (SmsFlashPromotionSession smsFlashPromotionSession : list1) {
            BeanUtils.copyProperties(smsFlashPromotionSession, smsFlashPromotionSessionDto);
            LambdaQueryWrapper<SmsFlashPromotionProductRelation> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(SmsFlashPromotionProductRelation::getFlashPromotionSessionId, smsFlashPromotionSession.getId());
            List<SmsFlashPromotionProductRelation> list2 = iSmsFlashPromotionProductRelationService.list(queryWrapper);//* 商品限时购与商品关系表

            ArrayList<PmsProduct> pmsProductList = new ArrayList<>();            //子类集合
            for (SmsFlashPromotionProductRelation smsFlashPromotionProductRelation : list2) {  //遍历 商品限时购与商品关系表
                LambdaQueryWrapper<PmsProduct> queryWrapper1 = new LambdaQueryWrapper<>();
                queryWrapper1.eq(PmsProduct::getId, smsFlashPromotionProductRelation.getProductId());
                queryWrapper1.eq(PmsProduct::getDeleteStatus, 0);
//                queryWrapper1.eq(PmsProduct::getPublishStatus, 1);
//                queryWrapper1.eq(PmsProduct::getVerifyStatus, 1);
                queryWrapper1.orderByAsc(PmsProduct::getSort);
                PmsProduct pmsProduct = iPmsProductService.getOne(queryWrapper1);
                pmsProductList.add(pmsProduct);

            }

            smsFlashPromotionSessionDto.setProductList(pmsProductList);
        }


        //3.最新商品列表 newProductList


        LambdaQueryWrapper<SmsHomeNewProduct> queryWrapper5 = new LambdaQueryWrapper<>();
        queryWrapper5.orderByAsc(SmsHomeNewProduct::getSort);
        queryWrapper5.eq(SmsHomeNewProduct::getRecommendStatus, 1);
        List<SmsHomeNewProduct> newProductLists = newProductService.list(queryWrapper5);


        ArrayList<PmsProduct> newProductList = new ArrayList<>();

        for (SmsHomeNewProduct smsHomeNewProduct : newProductLists) {
            Long productId = smsHomeNewProduct.getProductId();
            LambdaQueryWrapper<PmsProduct> lqw = new LambdaQueryWrapper<>();
            lqw.orderByAsc(PmsProduct::getSort);

            lqw.eq(PmsProduct::getNewStatus, 1);
            lqw.eq(PmsProduct::getDeleteStatus, 0);
            lqw.eq(PmsProduct::getPublishStatus, 1);
//         lqw.eq(PmsProduct::getVerifyStatus,1);
            lqw.eq(StringUtils.isNotEmpty(productId.toString()), PmsProduct::getId, productId);
            PmsProduct pmsProduct = productService.getOne(lqw);

            newProductList.add(pmsProduct);
        }

        ArrayList<PmsProduct> list2 = new ArrayList<>();

        if (newProductList.size() > 4) {
            for (int i = 0; i < 4; i++) {
                list2.add(newProductList.get(i));
            }
        } else {
            list2.addAll(newProductList);
        }


        //5.最热商品 hotProductList


        LambdaQueryWrapper<SmsHomeRecommendProduct> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.orderByAsc(SmsHomeRecommendProduct::getSort);
        queryWrapper1.eq(SmsHomeRecommendProduct::getRecommendStatus, 1);
        List<SmsHomeRecommendProduct> list = recommendProductService.list(queryWrapper1);

        ArrayList<PmsProduct> hotProductList = new ArrayList<>();

        for (SmsHomeRecommendProduct smsHomeRecommendProduct : list) {
            Long productId = smsHomeRecommendProduct.getProductId();
            LambdaQueryWrapper<PmsProduct> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(PmsProduct::getDeleteStatus, 0);
            queryWrapper.eq(PmsProduct::getPublishStatus, 1);
//            queryWrapper.eq(PmsProduct::getVerifyStatus,1);
            queryWrapper.eq(StringUtils.isNotEmpty(productId.toString()), PmsProduct::getId, productId);
            PmsProduct pmsProduct = productService.getOne(queryWrapper);

            hotProductList.add(pmsProduct);
        }

        ArrayList<PmsProduct> list3 = new ArrayList<>();
        if (hotProductList.size() > 5) {
            for (int i = 0; i < 4; i++) {
                list3.add(hotProductList.get(i));
            }
        }else{
            list3.addAll(hotProductList);
        }




        //6.推荐专题 subjectList

//        List<CmsSubject> subjectList = iCmsSubjectService.list();

        LambdaQueryWrapper<SmsHomeRecommendSubject> queryWrapper6 = new LambdaQueryWrapper<>();
        queryWrapper6.orderByAsc(SmsHomeRecommendSubject::getSort);
        queryWrapper6.eq(SmsHomeRecommendSubject::getRecommendStatus, 1);
        List<SmsHomeRecommendSubject> SmsHomeRecommendSubjectList = iSmsHomeRecommendSubjectService.list(queryWrapper6);


        ArrayList<CmsSubject> subjectList = new ArrayList<>();

        for (SmsHomeRecommendSubject homeRecommendSubject : SmsHomeRecommendSubjectList) {
            Long subjectId = homeRecommendSubject.getSubjectId();
            LambdaQueryWrapper<CmsSubject> cmsSubjectLambdaQueryWrapper = new LambdaQueryWrapper<>();
            cmsSubjectLambdaQueryWrapper.eq(StringUtils.isNotEmpty(subjectId.toString()), CmsSubject::getId, subjectId);
            CmsSubject cmsSubject = iCmsSubjectService.getOne(cmsSubjectLambdaQueryWrapper);
            cmsSubject.setRecommendStatus(1);
            subjectList.add(cmsSubject);
        }

        ArrayList<CmsSubject> list6 = new ArrayList<>();
        if (subjectList.size() > 5) {
            for (int i = 0; i < 5; i++) {
                list6.add(subjectList.get(i));
            }
        }else {
            list6.addAll(subjectList);
        }




        HashMap<Object, Object> map = new HashMap<>();
        map.put("advertiseList", list5);
        map.put("brandList", list4);
        map.put("homeFlashPromotion", smsFlashPromotionSessionDto);
        map.put("newProductList", list2);
        map.put("hotProductList", list3);
        map.put("subjectList", list6);

        return Result.success(map, "操作成功");
    }


    /**
     * 最新產品
     *
     * @param
     * @return
     */
    @GetMapping("/newProductList")
    public Result<List<PmsProduct>> newProductList() {



        LambdaQueryWrapper<SmsHomeNewProduct> queryWrapper5 = new LambdaQueryWrapper<>();
        queryWrapper5.orderByAsc(SmsHomeNewProduct::getSort);
        queryWrapper5.eq(SmsHomeNewProduct::getRecommendStatus, 1);
        List<SmsHomeNewProduct> newProductLists = newProductService.list(queryWrapper5);

        ArrayList<PmsProduct> newProductList = new ArrayList<>();
        for (SmsHomeNewProduct smsHomeNewProduct : newProductLists) {
            Long productId = smsHomeNewProduct.getProductId();
            LambdaQueryWrapper<PmsProduct> lqw = new LambdaQueryWrapper<>();
            lqw.orderByAsc(PmsProduct::getSort);

            lqw.eq(PmsProduct::getNewStatus, 1);
            lqw.eq(PmsProduct::getDeleteStatus, 0);
            lqw.eq(PmsProduct::getPublishStatus, 1);
//             lqw.eq(PmsProduct::getVerifyStatus,1);
            lqw.eq(StringUtils.isNotEmpty(productId.toString()), PmsProduct::getId, productId);
            PmsProduct pmsProduct = productService.getOne(lqw);

            newProductList.add(pmsProduct);
        }

        return Result.success(newProductList, "操作成功");
    }

    /**
     * 最熱商品
     *
     * @param request
     * @return
     */
    @GetMapping("/hotProductList")
    public Result<List<PmsProduct>> hotProductList(HttpServletRequest request) {


        LambdaQueryWrapper<SmsHomeRecommendProduct> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.orderByAsc(SmsHomeRecommendProduct::getSort);
        queryWrapper1.eq(SmsHomeRecommendProduct::getRecommendStatus, 1);
        List<SmsHomeRecommendProduct> list = recommendProductService.list(queryWrapper1);

        ArrayList<PmsProduct> hotProductList = new ArrayList<>();
        for (SmsHomeRecommendProduct smsHomeRecommendProduct : list) {
            Long productId = smsHomeRecommendProduct.getProductId();
            LambdaQueryWrapper<PmsProduct> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(PmsProduct::getDeleteStatus, 0);
            queryWrapper.eq(PmsProduct::getPublishStatus, 1);
//            queryWrapper.eq(PmsProduct::getVerifyStatus,1);
            queryWrapper.eq(StringUtils.isNotEmpty(productId.toString()), PmsProduct::getId, productId);
            PmsProduct pmsProduct = productService.getOne(queryWrapper);

            hotProductList.add(pmsProduct);
        }

        return Result.success(hotProductList, "操作成功");
    }

    /**
     * 获取首页商品分类
     *
     * @param parentId
     * @param
     * @return
     */
    @GetMapping("/productCateList/{parentId}")
    public Result productCateList(@PathVariable Long parentId ) {

        if (StringUtils.isNotEmpty(parentId.toString())) {
            List<PmsProductCategory> productCateList = iPmsProductService.groupBy(parentId);

            return Result.success(productCateList);
        }

        return Result.error("参数错误");
    }

    /**
     * 分页获取推荐商品
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/recommendProductList")
    public Result recommendProductList(@RequestParam int pageNum, @RequestParam int pageSize) {
        //1.分页构造器
        Page<PmsProduct> page = new Page<>(pageNum, pageSize);
        //2.条件构造器
        LambdaQueryWrapper<PmsProduct> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(PmsProduct::getSort);
        Page<PmsProduct> pmsProductPage = iPmsProductService.page(page, queryWrapper);

        List<PmsProduct> records = pmsProductPage.getRecords();

        ArrayList<PmsProduct> list = new ArrayList<>();
        for (PmsProduct record : records) {
            PmsProduct pmsProduct = new PmsProduct();
            BeanUtils.copyProperties(record, pmsProduct);
            list.add(pmsProduct);
        }
        return Result.success(list);
    }

    /**
     * 根据分类获取专题
     *
     * @param cateId
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/subjectList")
    public Result subjectList(@RequestParam Long cateId, int pageNum, int pageSize) {

        //1.分页构造器
        Page<CmsSubject> CmsSubjectPage = new Page<>(pageNum, pageSize);

        //2.条件构造器
        LambdaQueryWrapper<CmsSubject> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotEmpty(cateId.toString()), CmsSubject::getCategoryId, cateId);

        //3.
        Page<CmsSubject> cmsSubjectPage = iCmsSubjectService.page(CmsSubjectPage, queryWrapper);

        ArrayList<CmsSubject> subjectList = new ArrayList<>();
        List<CmsSubject> records = cmsSubjectPage.getRecords();

        for (CmsSubject record : records) {

            subjectList.add(record);

        }
        return Result.success(subjectList);

    }

}
